import java.util.*;

public class Text {
    public String decodeMessage(String key, String message) {
        Map<Character,Character> hashmap = new HashMap<>();
        char s = 'a';
        for(int i = 0; i < key.length(); i++) {
            char c = key.charAt(i);
            if(c != ' ' && !hashmap.containsKey(c)){
                hashmap.put(key.charAt(i),s++);
            }
        }


        StringBuilder str = new StringBuilder();
        for(int i = 0; i < message.length(); i++) {
            char c = message.charAt(i);
            if(c != ' ') {
                c = hashmap.get(c);
            }
            str.append(c);
        }
        return str.toString();
    }
    List<String> ret = new ArrayList<>();
    public boolean check(String s,int start,int end) {
        int num = 0;
        if(start > end ) {
            return false;
        }
        if(end - start >= 1 && s.charAt(start) == '0') {
            return false;
        }
        for(int i = start; i <= end; i++) {
            if(s.charAt(i) < '0' || s.charAt(i) > '9') {
                return false;
            }

            num = num*10 + (s.charAt(i)-'0');
            if(num > 255) {
                return false;
            }
        }
        return true;
    }
    public void func(String s,int startIndex,int Point) {
        if(Point == 3) {
            if(check(s,startIndex,s.length()-1)) {
                ret.add(s);
            }
            return;
        }
        for(int i = startIndex; i < s.length(); i++) {
            if(check(s,startIndex,i)) {
                s = s.substring(0,i+1)+ "." + s.substring(i+1);
                Point++;
            }else{
                continue;
            }
            func(s,i+2,Point);
            Point--;
            s = s.substring(0,i+1) + s.substring(i+2);
        }

    }
    public List<String> restoreIpAddresses(String s) {
        func(s,0,0);
        return ret;
    }

}
